package cn.hejiang.mamis.core.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hejiang.mamis.core.entity.PlatUser;
import cn.hejiang.mamis.core.entity.SysMenu;
import cn.hejiang.mamis.core.entity.SysRole;
import cn.hejiang.mamis.core.mapper.SysRoleMapper;
import cn.hejiang.mamis.core.service.SysRoleService;

@Service
@CacheConfig(keyGenerator = "cacheKeyGenerator")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
	@Transactional
	@CacheEvict(cacheNames = { "SYS_ROLE_MENU", "SYS_USER_MENU" }, allEntries = true)
	public int saveRoleMenus(String roleId, Set<String> menuIdSet, String sysId, String curUserId) {
		this.getBaseMapper().clearRoleMenu(roleId, sysId);

		int ret = 0;
		for (String menuId : menuIdSet) {
			ret += this.getBaseMapper().addRoleMenu(roleId, menuId, sysId, curUserId);
		}

		return ret;
	}

	@Cacheable(cacheNames = "SYS_ROLE")
	public List<SysRole> getDeptRoleList(String deptId, String sysId) {
		return this.getBaseMapper().getDeptRoleList(deptId, sysId);
	}

	@Cacheable(cacheNames = "SYS_ROLE_USER")
	public List<PlatUser> getRoleUserList(String roleId, String sysId) {
		return this.getBaseMapper().getRoleUserList(roleId, sysId);
	}

	@Cacheable(cacheNames = "SYS_ROLE_USER")
	public List<PlatUser> getRoleUserCandidates(String roleId, String sysId) {
		return this.getBaseMapper().getRoleUserCandidates(roleId, sysId);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE_USER", "SYS_USER_MENU" }, allEntries = true)
	public int addRoleUser(String roleId, String userId, String sysId, String curUserId) {
		return this.getBaseMapper().addRoleUser(roleId, userId, sysId, curUserId);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE_USER", "SYS_USER_MENU" }, allEntries = true)
	public int delRoleUser(String roleId, String userId, String sysId) {
		return this.getBaseMapper().delRoleUser(roleId, userId, sysId);
	}

	@Cacheable(cacheNames = "SYS_ROLE_MENU")
	public List<SysMenu> getRoleMenuList(String roleId, String sysId) {
		return this.getBaseMapper().getRoleMenuList(roleId, sysId);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE_MENU", "SYS_USER_MENU" }, allEntries = true)
	public int clearRoleMenu(String roleId, String sysId) {
		return this.getBaseMapper().clearRoleMenu(roleId, sysId);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE_MENU", "SYS_USER_MENU" }, allEntries = true)
	public int addRoleMenu(String roleId, String menuId, String sysId, String curUserId) {
		return this.getBaseMapper().addRoleMenu(roleId, menuId, sysId, curUserId);
	}

	@Cacheable(cacheNames = "SYS_ROLE")
	public SysRole getRoleByName(String roleName, String sysId, String deptId) {
		return this.getBaseMapper().getRoleByName(roleName, sysId, deptId);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE_USER", "SYS_USER_MENU" }, allEntries = true)
	public int clearRoleUser(String roleId, String sysId) {
		return this.getBaseMapper().clearRoleUser(roleId, sysId);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE", "SYS_ROLE_USER", "SYS_ROLE_MENU", "SYS_USER_MENU" }, allEntries = true)
	public boolean update(SysRole entity, Wrapper<SysRole> updateWrapper) {
		return super.update(entity, updateWrapper);
	}

	@CacheEvict(cacheNames = { "SYS_ROLE", "SYS_ROLE_USER", "SYS_ROLE_MENU", "SYS_USER_MENU" }, allEntries = true)
	public boolean save(SysRole entity) {
		return super.save(entity);
	}
}
